class OuterClass{
    public int data1=1;
    private int data2=2;
    public static int data3=3;

    static class InnerClass{
        public int data4=4;
        private int data5=5;
        public static int data6=6;

        public void test(){
           //System.out.println(data1);//System.out.println(data2);
            //没办法打印data1和data2因为他俩是非静态的，需要依赖对象
            OuterClass outerClass=new OuterClass();
            System.out.println(outerClass.data1);
            System.out.println(outerClass.data2);

            System.out.println(data3);
            System.out.println(data4);
            System.out.println(data5);
            System.out.println(data6);
            System.out.println("InnerClass::test()");
        }
    }

    class InnerClass1{
        public int data1=99;//内部类与外部类变量同名，优先内部类
        public int data7=7;
        private int data8=8;
        public static int data9=9;

        public void test(){
            //OutClass.this 外部类的this
            System.out.println(OuterClass.this.data1);
            System.out.println(this.data1);
            System.out.println(OuterClass.this.data2);
            System.out.println(data3);
            System.out.println(data7);
            System.out.println(data8);
            System.out.println(data9);
            System.out.println("InnerClass1::test()");
        }
    }
}


class OuterClass1{
    public void test(){
        System.out.println("OuterClass1::test()");
    }
}


interface ITest{
    void test();
}


public class TestDemo {
    public static void main(String[] args) {
        //静态内部类在实例化时要通过外部类来实例化
        OuterClass.InnerClass innerClass=new OuterClass.InnerClass();
        innerClass.test();

        //实例内部类在实例化时要通过外部类的对象
        OuterClass outerClass=new OuterClass();
        OuterClass.InnerClass1 innerClass1=outerClass.new InnerClass1();
        innerClass1.test();

        //匿名内部类
        OuterClass1 out=new OuterClass1(){
            @Override
            public void test() {
                System.out.println("重写test方法");
            }
        };
        out.test();
        //或
        new OuterClass1(){
            @Override
            public void test() {
                System.out.println("重写test方法！！！");
            }
        }.test();
        //接口也可以像上面两个一样写两种匿名
        ITest iTest=new ITest(){
            @Override
            public void test() {
                System.out.println("重写test。。。");
            }
        };
        iTest.test();

        //局部内部类
        /*class Out{
            public void func(){
                class In{
                    public int data;
                }
                //只能在该方法中使用
                In in=new In();
                System.out.println("data="+in.data);
            }
        }直接放在这里没有打印*/
        class Out{
            public void func(){
                class In{
                    public int data;
                }
                //只能在该方法中使用
                In in=new In();
                System.out.println("data="+in.data);
            }
        }
        Out out2=new Out();
        out2.func();

    }
}
